import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.Color;
/*You can buy or not buy, none auctioning !*/
public class UtilityUI extends JPanel {
	private BoardUi board;
	private JButton btnBuy, btnNotBuy, btnPay;
	private JLabel label;
	private Player p;
	private Block[] bl;
	private Utility utility;
	private Utility[] utilArray;
	private int currentRent;

		public UtilityUI(Player currentPlayer, Utility util, Block[] blk, BoardUi board_) {
			setBackground(new Color(230, 230, 250));
			this.p = currentPlayer;
			this.utility = util;
			this.bl=blk;
			this.utilArray = new Utility[2];
			this.board=board_;
			
			this.setLayout(null);
			
			label = new JLabel("");
			label.setIcon(new ImageIcon(getClass().getResource(this.utility.getUtilityDeed())));
			label.setBounds(0, 0, 199, 200);
			label.setVisible(true);
			this.add(label);
			
			btnBuy = new JButton("Bilin");
			btnBuy.setFont(new Font("Tahoma", Font.BOLD, 11));
			btnBuy.setBounds(199, 68, 71, 23);
			btnBuy.setEnabled(false);
			btnBuy.setVisible(false);
			btnBuy.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					buy();
				}
			});
			this.add(btnBuy);
			
			btnPay = new JButton("Renta");
			btnPay.setFont(new Font("Tahoma", Font.BOLD, 11));
			btnPay.setBounds(199, 103, 71, 23);
			btnPay.setEnabled(false);
			btnPay.setVisible(false);
			btnPay.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					pay();
				}
			});
			this.add(btnPay);
			
			btnNotBuy = new JButton("Return");
			btnNotBuy.setFont(new Font("Tahoma", Font.BOLD, 11));
			btnNotBuy.setBounds(199, 137, 100, 23);
			btnNotBuy.setEnabled(false);
			btnNotBuy.setVisible(false);
			btnNotBuy.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					returnToGame();
				}
			});
			this.add(btnNotBuy);
			
			this.checkOwner();
			this.fillUtilityArray();
			this.getOwnedUtilityGroup();
		}

		public void buy() {
			if(board.getCurrentPlayer().getMoney()>= utility.getPrice()){
			utility.setOwner(p);
			p.subtractMoney(utility.getPrice());
			btnBuy.setEnabled(false); //you can only buy it one time
			btnBuy.setVisible(false);
			board.setCurrentPlayer();
			board.getPlayerPanelUI().updatePlayerMoney();
			board.getPlayerPanelUI().updateButtons(this.p.getPlayerCount(), this.p.getCurrentPosition());
			}else{
				JOptionPane.showMessageDialog(null,"You don't have enough Money to buy!");
			}
		}
		
		public void returnToGame(){
			board.getDiceUI().rolledDoubles();
		//  board.getGameButtonUI().enableEndTurnBtn();
			setVisible(false);
		}
		
		public void pay(){
			if(board.getCurrentPlayer().getMoney()>= (p.getLastRoll()*currentRent)){
			p.subtractMoney(p.getLastRoll()*currentRent);
			utility.getOwner().addMoney(currentRent);
			btnPay.setEnabled(false);	//you can pay one time per turn
			btnPay.setVisible(false);
			btnNotBuy.setEnabled(true);
			btnNotBuy.setVisible(true);
			board.setCurrentPlayer();
			board.getPlayerPanelUI().updatePlayerMoney();
			}else{
					BankruptcyChecker bc = new BankruptcyChecker(p, bl);
					int worth=bc.checkWorth();
					
					if(worth>=currentRent){
						JOptionPane.showMessageDialog(null,"You don't have enough Money to pay rent, just mortgage your properties");
					}else{
						bc.transferAssetsPlayer();
						board.getCurrentPlayer().setActive(false);
						JOptionPane.showMessageDialog(null,"You don't have enough Money to pay rent, Out ka na!");
						btnNotBuy.setVisible(true);			
					}
			}
			
		}
		
		public void checkOwner(){
			if(this.utility.getOwner()==null){
				btnBuy.setEnabled(true);
				btnBuy.setVisible(true);
				btnNotBuy.setEnabled(true);
				btnNotBuy.setVisible(true);
			}else if(this.utility.getOwner()!=this.p){
				btnPay.setEnabled(true);
				btnPay.setVisible(true);
			}else if(this.utility.getOwner()==this.p){
				btnNotBuy.setEnabled(true);
				btnNotBuy.setVisible(true);
			}
		}
		
		public void fillUtilityArray() {
			int utilCounter = 0;
			for(int i=0;i<this.bl.length;i++){
				if(this.bl[i] instanceof Utility){
					this.utilArray[utilCounter]=(Utility)bl[i];
					utilCounter++;
				}
			}	
		}
		
		public void getOwnedUtilityGroup(){
			int ownedCounter = 0;
			
			for(int i=0; i<this.utilArray.length; i++){
				if(this.utilArray[i].getUtilityGrp() ==this.utility.getUtilityGrp()){//match fist
					if(this.utilArray[i].getOwner()==this.utility.getOwner()&& this.utility.getOwner()!=null){//match second
						ownedCounter++;
					}
				}
			}
			if(2==ownedCounter){
				this.currentRent= 10;
			}else if(1==ownedCounter){
				this.currentRent= 4;
			}
		}
	}
